package fpt.edu.vn.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.UsersDao;
import fpt.edu.vn.model.Locations;
import fpt.edu.vn.model.Locationtypes;
import fpt.edu.vn.model.Provinces;
import fpt.edu.vn.model.Roles;
import fpt.edu.vn.model.Users;

/* 
 * AdminUserDoAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class AdminUserDoAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String email;

	private String Username;
	private int usersId;

	private String displayName;

	private Date createDate;

	private Date lastLoginDate;

	private InputStream inputStream;
	private int isBanner;
	private String password;
	private int roleId;
	private int roleIdE;
	private String avatar;
	private int sizelist;
	private String search_email;
	private String fromregdate;
	private String toregdate;
	private String search_name;
	private String fromentdate;
	private String toentdate;
	private String usernameDelete;
	private int search_banned;
	private int search_reglevel;
	private String search_order_r;
	private String search_order_l;
	private List<Roles> lstRoles;
	private List<Object> lstusers;
	private UsersDao usernewdao = new UsersDao();
	// For SessionAware
	Map<String, Object> session;

	public String adminDeleteUser() {
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {

				if (0 != usersId) {

					usernameDelete = usernewdao.listdeleteuser(usersId).get(0)
							.getDisplayName();
					if (usernewdao.deletedUser(usersId)) {
						lstusers = usernewdao
								.searchUser(search_email, fromregdate,
										toregdate, search_name, fromentdate,
										toentdate, search_banned,
										search_reglevel, search_order_r,
										search_order_l);
						sizelist = lstusers.size();
						lstRoles = usernewdao.listRoles();
					} else {
						return NONE;
					}

				}
				return SUCCESS;
			}
		}
		return ERROR;
	}

	public String adminAddUser() {
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {
				if (usernewdao.emailSearch(email)) {
					Users user = new Users();
					user.setEmail(email);
					user.setDisplayName(displayName);
					user.setRoleId(roleId);
					user.setPassword(password);
					user.setCreateDate(getSysdate());
					user.setLastLoginDate(getSysdate());
					user.setIsBanner(0);
					user.setAvatar("anonymous.png");
					usernewdao.add(user);
				}
				inputStream = toUTF8InputStream("Add " + displayName
						+ " thành công!");

				return SUCCESS;
			}
		}
		return ERROR;

	}
	public String adminEditUser() {
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {
				lstusers = usernewdao.listProfile(usersId);
				lstRoles = usernewdao.listRoles();
				
				return SUCCESS;
			}
		}
		return ERROR;

	}
	public String execute() {
		session = ActionContext.getContext().getSession();
		if (session.containsKey("userRoleIdss")) {
			email = (String) session.get("emailss");
			int isAdmin = (Integer) session.get("userRoleIdss");
			if (isAdmin == 1) {

				// get district

				lstusers = usernewdao.searchUser(search_email, fromregdate,
						toregdate, search_name, fromentdate, toentdate,
						search_banned, search_reglevel, search_order_r,
						search_order_l);
				sizelist = lstusers.size();
				lstRoles = usernewdao.listRoles();

				// sizelist =lstusers.size();
				return SUCCESS;
			}
		}
		return ERROR;

	}

	public static InputStream toUTF8InputStream(String str) {
		InputStream is = null;
		try {
			is = new ByteArrayInputStream(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// UTF-8 should always be supported
			throw new AssertionError();
		}
		return is;
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}
	
	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public int getRoleIdE() {
		return roleIdE;
	}

	public void setRoleIdE(int roleIdE) {
		this.roleIdE = roleIdE;
	}

	public String getUsernameDelete() {
		return usernameDelete;
	}

	public void setUsernameDelete(String usernameDelete) {
		this.usernameDelete = usernameDelete;
	}

	public List<Roles> getLstRoles() {
		return lstRoles;
	}

	public void setLstRoles(List<Roles> lstRoles) {
		this.lstRoles = lstRoles;
	}

	public String getSearch_email() {
		return search_email;
	}

	public void setSearch_email(String search_email) {
		this.search_email = search_email;
	}

	public String getFromregdate() {
		return fromregdate;
	}

	public void setFromregdate(String fromregdate) {
		this.fromregdate = fromregdate;
	}

	public String getToregdate() {
		return toregdate;
	}

	public void setToregdate(String toregdate) {
		this.toregdate = toregdate;
	}

	public String getSearch_name() {
		return search_name;
	}

	public void setSearch_name(String search_name) {
		this.search_name = search_name;
	}

	public String getFromentdate() {
		return fromentdate;
	}

	public void setFromentdate(String fromentdate) {
		this.fromentdate = fromentdate;
	}

	public String getToentdate() {
		return toentdate;
	}

	public void setToentdate(String toentdate) {
		this.toentdate = toentdate;
	}

	public int getSearch_banned() {
		return search_banned;
	}

	public void setSearch_banned(int search_banned) {
		this.search_banned = search_banned;
	}

	public int getSearch_reglevel() {
		return search_reglevel;
	}

	public void setSearch_reglevel(int search_reglevel) {
		this.search_reglevel = search_reglevel;
	}

	public String getSearch_order_r() {
		return search_order_r;
	}

	public void setSearch_order_r(String search_order_r) {
		this.search_order_r = search_order_r;
	}

	public String getSearch_order_l() {
		return search_order_l;
	}

	public void setSearch_order_l(String search_order_l) {
		this.search_order_l = search_order_l;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}

	public int getUsersId() {
		return usersId;
	}

	public void setUsersId(int usersId) {
		this.usersId = usersId;
	}

	public int getSizelist() {
		return sizelist;
	}

	public void setSizelist(int sizelist) {
		this.sizelist = sizelist;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	public int getIsBanner() {
		return isBanner;
	}

	public void setIsBanner(int isBanner) {
		this.isBanner = isBanner;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public List<Object> getLstusers() {
		return lstusers;
	}

	public void setLstusers(List<Object> lstusers) {
		this.lstusers = lstusers;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return Username;
	}

	public void setUsername(String username) {
		Username = username;
	}
}
